<?php

namespace ABonus\Bundle\ExtractBundle\Command;

use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\DomCrawler\Crawler;
use ABonus\Bundle\ExtractBundle\Entity;

class ImportBlocCommand extends ContainerAwareCommand
{
    protected $imagePath = "http://bleau.info/images/";

    protected function configure()
    {
        $this
        ->setName('abonus:import:bloc')
        ->setDescription('Import a bloc')
        ->addArgument('bloc-url', InputArgument::REQUIRED, 'url of the bloc')
        ->addArgument('sector-name', InputArgument::OPTIONAL, 'name of the sector')
        ;

    }

    protected function execute(InputInterface $input, OutputInterface $output)
    {
        $blocUrl = $input->getArgument('bloc-url');
        $sectorName = $input->getArgument('sector-name');
        $html = file_get_contents($blocUrl);
        $crawler = new Crawler();
        $crawler->addHtmlContent($html);

        $this->createEntityFromCrawler($crawler, $sectorName);
    }


    protected function getBoulderName($crawler){
        $boulderNameAndGrade = $crawler->filter('#mainsub1 > h2')->text();
        $beginningOfGrade = $this->getPositionOfFirstNumericChar($boulderNameAndGrade);
        $boulderName = trim(substr($boulderNameAndGrade, 0, $beginningOfGrade));
        return $boulderName;
    }

    protected function getBoulderGrade($crawler){
        $boulderNameAndGrade = $crawler->filter('#mainsub1 > h2')->text();
        $beginningOfGrade = $this->getPositionOfFirstNumericChar($boulderNameAndGrade);
        $gradeStringIndex = $beginningOfGrade-(strlen($boulderNameAndGrade));
        $boulderGrade = trim(substr($boulderNameAndGrade, $gradeStringIndex));
        return $boulderGrade;
    }

    protected function getBoulderTypes($crawler){
        $baseStringWithEverything = $crawler->filter('#mainsub1 > p')->html();
        $boulderTypes = $this->get_string_between($baseStringWithEverything, "<br>type : <b>", "</b>");
        return $boulderTypes;
    }

    protected function getBoulderDescription($crawler){
        $baseStringWithEverything = $crawler->filter('#mainsub1')->text();
        if(strpos($baseStringWithEverything, 'grade')){
            $boulderDescription = $this->get_string_between($baseStringWithEverything, "description :", "grade");
        }
        else if(strpos($baseStringWithEverything, 'see')){
            $boulderDescription = $this->get_string_between($baseStringWithEverything, "description :", "see");
        }
        return $boulderDescription;
    }

    protected function getPositionOfFirstNumericChar($string){
        preg_match('/^\D*(?=\d)/', $string, $m);
        return isset($m[0]) ? strlen($m[0]) : false;
    }

    protected function get_string_between($string, $start, $end){
        $string = " ".$string;
        $ini = strpos($string,$start);
        if ($ini == 0) return "";
        $ini += strlen($start);
        $len = strpos($string,$end,$ini) - $ini;
        return substr($string,$ini,$len);
    }


    protected function getBoulderImage($crawler){
        $baseStringWithEverything = $crawler->filter('#mainsub1')->html();
        $imagePath =  trim($this->get_string_between($baseStringWithEverything, "/images/", "\""));
        if($imagePath != ""){
            $imagePathWithHost = $this->imagePath.$imagePath;
        }else{
            return null;
        }
        return $imagePathWithHost;
    }

    protected function createEntityFromCrawler($crawler, $sectorName){
        $blocManager = $this->getService('bloc_manager');
        $sectorManager = $this->getService('sector_manager');
        $sector = $sectorManager->getSectorFromName($sectorName);
        $bloc = new Entity\Bloc();
        $bloc->setName($this->getBoulderName($crawler));
        $bloc->setGrade($this->getBoulderGrade($crawler));
        $bloc->setTypes($this->getBoulderTypes($crawler));
        $bloc->setDescription($this->getBoulderDescription($crawler));
        $bloc->setSector($sector);
        $imageFromBloc = $this->getBoulderImage($crawler);
        if($imageFromBloc != null)
        {
            $blocManager->createImageAndAttachItToBloc($bloc, $imageFromBloc);
        }
        $blocManager->saveBloc($bloc);
    }

    protected function getService($service){
        return $this->getContainer()->get($service);
    }
}
